# -*- coding: utf-8 -*-
import datetime
import time
import tushare as ts
import pymongo
import json
import random
from mongo_config import *
from multiprocessing import Pool

mongo_client = pymongo.MongoClient(MONGO_URL, 27017)
db = mongo_client[MONGO_DB_RES]

def get_5minute(code):
    dictlist = []
    stock = code + '_5'
    collection_res = db[stock]
    data = ts.get_k_data(code, ktype='5')[-10:-1]
    for index, datum in data.iterrows():
        dicts = {}
        dicts['_id'] = int(time.mktime(time.strptime(datum['date'], "%Y-%m-%d %H:%M")))
        dicts['open'] = datum['open']
        dicts['close'] = datum['close']
        dicts['high'] = datum['high']
        dicts['low'] = datum['low']
        dictlist.append(dicts)
        # print(dicts)
        # try:
        #     collection_res.insert_one(dicts)
        # except:
        #     pass
    print(code, ':', dictlist)
    try:
        collection_res.insert_many(dictlist)
    except:
        pass

def get_15minute(code):
    re_data = {}
    dictlist = []
    stock = code + '_15'
    data = ts.get_k_data(code, ktype='15')[-5:-1]
    for index, datum in data.iterrows():
        dicts = {}
        dicts['_id'] = int(time.mktime(time.strptime(datum['date'], "%Y-%m-%d %H:%M")))
        dicts['open'] = datum['open']
        dicts['close'] = datum['close']
        dicts['high'] = datum['high']
        dicts['low'] = datum['low']
        dictlist.append(dicts)
    # print(dictlist)
    re_data[stock] = dictlist
    return re_data
    # print(code,':',dictlist)
    # try:
    #     collection_res.insert_many(dictlist, ordered=False)
    # except:
    #     pass

def get_30minute(code):
    re_data = {}
    dictlist = []
    stock = code + '_30'
    data = ts.get_k_data(code, ktype='30')[-5:-1]
    for index, datum in data.iterrows():
        dicts = {}
        dicts['_id'] = int(time.mktime(time.strptime(datum['date'], "%Y-%m-%d %H:%M")))
        dicts['open'] = datum['open']
        dicts['close'] = datum['close']
        dicts['high'] = datum['high']
        dicts['low'] = datum['low']
        dictlist.append(dicts)
    re_data[stock] = dictlist
    return re_data
    # print(code, ':', dictlist)
    # try:
    #     collection_res.insert_many(dictlist, ordered=False)
    # except:
    #     pass

def get_1h(code):
    re_data = {}
    dictlist = []
    stock = code + '_1h'
    data = ts.get_k_data(code, ktype='60')[-3:-1]
    for index, datum in data.iterrows():
        dicts = {}
        dicts['_id'] = int(time.mktime(time.strptime(datum['date'], "%Y-%m-%d %H:%M")))
        dicts['open'] = datum['open']
        dicts['close'] = datum['close']
        dicts['high'] = datum['high']
        dicts['low'] = datum['low']
        dictlist.append(dicts)
    re_data[stock] = dictlist
    return re_data
    # print(code, ':', dictlist)
    # try:
    #     collection_res.insert_many(dictlist, ordered=False)
    # except:
    #     pass

def get_1D(code):
    re_data = {}
    dictlist = []
    stock = code + '_1D'
    data = ts.get_k_data(code, ktype='D')[-3:-1]     #[-2:]
    for index, datum in data.iterrows():
        dicts = {}
        dicts['_id'] = int(time.mktime(time.strptime(datum['date'], "%Y-%m-%d")))
        dicts['open'] = datum['open']
        dicts['close'] = datum['close']
        dicts['high'] = datum['high']
        dicts['low'] = datum['low']
        dictlist.append(dicts)
    re_data[stock] = dictlist
    return re_data
    # print(code, ':', dictlist)
    # try:
    #     collection_res.insert_many(dictlist, ordered=False)
    # except:
    #     pass
def save_to_mongo(datas):
    # print(datas)
    for data in datas:
        key = [key for key in data][0]
        values = data[key]
        collection_res = db[key]
        try:
            collection_res.insert_many(values, ordered=False)
        except:
            pass

def main():
    stkcodes = ['300074', '002547', '603011', '600555', '002300', '002374', '600241', '002451', '300312', '603650',
                '300746', '300225', '002782', '002907', '000590', '603901', '002488', '002356', '300747', '603486',
                '300677', '300745', '300050', '002906', '002148', '002125', '000678', '603398', '300578', '603996',
                '603421', '300647', '002870', '300056', '300546', '600448', '300484', '000892', '601330', '000595',
                '002211', '300141', '300134', '002584', '600722', '002388', '000979', '300120', '300177', '600130',
                '600156', '002592', '601113', '300309', '002694', '300584', '300582', '002278', '603555', '300601',
                '002676', '300685', '300128', '600303', '603045', '300242', '000868', '300311', '603007', '300529',
                '300173', '600250', '300743', '002813', '000802', '002161', '300487', '300260', '002606', '002692',
                '000150', '000636', '600870', '600763', '603918', '000995', '002796', '600289', '000810', '000812',
                '300374', '603988', '300012', '000735', '002725', '002810', '300261', '603718', '300554', '000752',
                '300547', '603477', '600536', '600567', '000596', '000713', '600715', '603856', '300476', '300167',
                '002259', '002680', '002083', '002465', '000553', '300139', '300123', '603606', '300107', '002486',
                '300672', '002014', '000976', '002276', '300713', '300122', '002693', '600695', '002199', '600645',
                '002634', '603579', '600313', '300630', '000790', '300071', '300499', '600345', '300323', '600903',
                '600265', '600483', '002358', '300522', '002519', '603777', '600268', '600230', '300062', '600069',
                '000818', '000301', '600081', '002398', '002399', '600500', '002099', '600867', '600809', '300001',
                '300116', '002326', '603866', '000430', '002134', '603985', '600375', '600666', '000971', '603819',
                '002138', '300634', '002909', '601996', '601882', '603688', '600771', '002746', '300232', '002616',
                '000410', '603605', '300644', '603337', '600197', '600710', '300512', '300559', '600215', '600110',
                '002885', '300216', '603127', '002184', '002901', '300732', '002851', '002235', '600011', '300404',
                '600405', '600794', '002021', '300356', '300384', '300004', '300076', '002218', '603387', '300083',
                '600389', '002159', '002348', '002921', '603787', '002560', '002496', '000567', '603897', '600689',
                '002458', '603963', '600136', '002619', '002387', '002773', '000909', '000613', '002264', '300144',
                '002299', '600367', '002915', '002549', '600521', '300333', '603889', '002357', '300294', '002672',
                '300454', '002065', '300104', '300337', '002809', '300425', '300132', '300505', '002346', '000593',
                '002858', '600240', '300371', '601858', '603199', '002922', '002114', '600329', '002524', '603638',
                '002829', '603667', '603039', '603707', '000973', '002565', '300121', '603666', '300620', '000560',
                '603043', '600436', '300424', '600084', '002432', '002461', '603059', '603601', '002587', '002181',
                '300253', '603711', '002228', '002413', '002462', '002198', '002182', '300383', '000633', '002702',
                '002022', '600227', '600211', '000815', '600093', '300606', '603096', '002931', '300742', '002030',
                '002660', '002234', '300296', '000506', '603259', '300418', '002570', '002115', '000957', '002013',
                '300093', '002622', '300572', '002058', '002377', '300627', '300067', '002640', '002410', '603367',
                '002440', '300727', '603886', '603133', '600760', '600600', '600074', '000820', '300048', '300495',
                '300639', '600299', '002596', '300110', '600673', '300068', '600888', '601137', '603429', '000710',
                '603369', '600586', '000606', '000738', '300006', '601990', '300527', '300368', '600486', '600810',
                '002332', '600278', '603587', '002317', '002815', '603871', '601258', '300091', '300357', '300558',
                '300332', '300172', '300439', '600228', '603197', '002849', '002175', '600562', '300463', '000682',
                '002304', '300152', '603348', '002515', '300040', '000639', '000661', '300081', '300044', '300352',
                '300388', '300739', '002353', '002054', '300472', '603305', '600578', '002618', '600461', '601069',
                '300320', '600302', '002400', '000726', '300113', '300440', '300269', '002498', '002096', '002129',
                '300086', '002040', '600498', '300456', '300283', '002227', '002447', '300411', '002286', '002240',
                '600593', '300078', '002319', '002390', '002395', '600596', '002679', '300174', '002769', '300715',
                '603103', '600354', '002576', '600238', '002821', '601619', '600380', '002575', '002427', '300567',
                '601633', '000823', '300642', '600559', '603876', '000504', '002898', '002775', '300470', '002727',
                '600969', '002836', '600563', '300736', '603301', '000977', '300588', '002507', '000156', '000557',
                '000886', '300653', '300149', '002289', '600579', '601008', '002588', '600078', '603788', '300097',
                '002223', '600891', '300185', '002582', '600886', '002262', '300188', '603708', '002306', '000727',
                '603228', '603725', '600131', '002349', '002905', '600804', '600789', '300612', '601005', '002923',
                '603288', '002590', '002168', '000518', '300200', '000955', '000042', '300633', '300346', '600280',
                '002097', '002925', '002119', '600598', '603019', '300490', '600698', '300192', '002686', '603768',
                '600477', '300150', '300517', '600893', '300595', '000869', '600694', '300705', '600193', '300549',
                '002840', '000707', '600795', '600826', '603027', '002294', '002861', '603722', '000949', '600033',
                '000615', '600987', '600655', '000819', '002287', '002877', '600638', '601700', '002603', '300482',
                '002516', '000739', '600572', '002627', '000533', '600780', '601226', '600540', '002859', '300719',
                '002674', '603160', '600460', '300390', '600371', '601018', '002418', '000698', '600213', '000791',
                '600365', '300366', '002180', '002076', '603025', '300354', '300082', '300398', '300618', '002371',
                '600828', '300486', '000016', '002895', '002303', '002776', '002071', '603158', '603658', '300042',
                '000989', '002646', '603811', '000544', '600735', '002238', '002041', '002563', '002520', '300166',
                '002911', '603037', '603018', '300098', '600377', '002712', '002800', '603578', '600392', '300725',
                '002222', '600391', '002383', '603156', '002614', '603659', '300327', '603568', '300523', '002422',
                '300015', '002179', '300518', '603997', '603939', '603880', '603838', '603825', '603690', '603636',
                '603602', '603501', '603456', '603386', '603329', '603315', '603299', '603111', '603077', '603032',
                '603016', '601992', '601727', '601669', '601390', '601368', '601118', '601098', '601000', '600984',
                '600965', '600896', '600869', '600850', '600848', '600844', '600835', '600811', '600800', '600796',
                '600784', '600768', '600766', '600764', '600759', '600753', '600751', '600750', '600745', '600733',
                '600724', '600701', '600687', '600678', '600677', '600641', '600636', '600548', '600515', '600506',
                '600490', '600485', '600469', '600423', '600399', '600335', '600328', '600318', '600317', '600293',
                '600251', '600246', '600232', '600226', '600221', '600217', '600212', '600175', '600165', '600158',
                '600148', '600146', '600145', '600122', '600112', '600086', '600054', '600052', '600027', '600022',
                '300740', '300683', '300680', '300670', '300603', '300586', '300561', '300510', '300491', '300464',
                '300379', '300362', '300350', '300343', '300324', '300317', '300306', '300299', '300247', '300238',
                '300229', '300198', '300197', '300187', '300146', '300131', '300118', '300111', '300108', '300072',
                '300049', '300008', '002872', '002866', '002856', '002843', '002819', '002806', '002802', '002778',
                '002739', '002723', '002721', '002719', '002716', '002668', '002665', '002656', '002650', '002630',
                '002621', '002617', '002611', '002610', '002609', '002604', '002602', '002573', '002569', '002552',
                '002512', '002502', '002493', '002485', '002477', '002464', '002450', '002445', '002437', '002436',
                '002431', '002426', '002411', '002408', '002369', '002361', '002359', '002321', '002310', '002309',
                '002301', '002270', '002263', '002252', '002239', '002226', '002210', '002171', '002165', '002147',
                '002143', '002121', '002113', '002102', '002098', '002089', '002086', '002082', '002075', '002059',
                '002053', '002051', '002043', '002011', '000972', '000967', '000936', '000912', '000903', '000838',
                '000829', '000825', '000796', '000793', '000779', '000723', '000711', '000693', '000673', '000669',
                '000666', '000616', '000611', '000603', '000584', '000566', '000564', '000545', '000540', '000539',
                '000534', '000526', '000523', '000509', '000505', '000426', '000415', '000409', '000408', '000038',
                '000035', '000029', '000022', '000018', '000008', '002044', '601100', '000062', '300003', '603823',
                '300308', '603569', '000990', '603388', '002589', '600060', '603669', '603808', '002352', '002607',
                '603776', '000901', '000019', '000963', '603630', '000777', '601888', '300278', '002633', '603558',
                '601021', '600615', '603080', '002841', '002480', '002757', '600630', '000981', '000663', '002200',
                '300291', '002370', '300162', '603012', '601901', '000776', '600306', '002296', '000650', '300244',
                '002100', '600271', '002724', '603300', '300538', '600167', '300080', '300126', '600566', '000656',
                '002291', '600023', '002248', '002632', '002246', '002288', '600998', '603393', '300351', '600633',
                '002783', '002010', '002173', '600664', '002831', '603598', '600580', '002864', '600570', '300569',
                '002463', '600575', '600393', '002830', '600792', '300532', '600009', '002170', '002641', '300325',
                '603960', '600116', '300156', '002504', '300403', '600573', '300666', '300459', '002865', '600699',
                '600017', '601799', '603668', '000862', '600982', '300665', '600818', '002132', '600103', '002020',
                '002795', '002334', '002509', '600518', '603506', '002747', '603389', '601139', '000966', '600884',
                '600601', '601886', '300180', '600744', '600097', '002302', '000803', '002068', '002380', '000513',
                '600626', '300183', '000420', '000919', '600731', '002655', '600990', '600654', '600821', '002538',
                '603928', '000043', '300270', '000902', '603843', '603488', '002265', '603968', '300400', '600981',
                '000801', '603076', '600298', '300451', '300676', '002539', '002484', '002031', '000806', '300450',
                '600199', '002652', '002761', '000667', '300688', '002281', '002063', '600276', '300304', '300709',
                '000056', '600095', '002141', '601086', '300112', '600361', '600085', '603991', '601998', '601678',
                '600862', '002197', '300009', '002667', '300241', '002314', '300509', '000403', '300233', '600975',
                '600611', '300069', '002438', '002728', '300475', '300550', '601880', '002078', '002714', '300521',
                '600543', '300513', '300689', '600803', '300448', '300413', '002312', '300649', '000821', '600346',
                '002487', '603683', '600038', '603055', '603214', '300035', '002023', '002007', '000576', '002449',
                '002188', '002382', '300609', '300723', '002808', '000755', '600290', '600163', '000572', '600978',
                '002852', '600917', '300637', '601126', '002230', '002295', '600883', '300153', '603929', '002737',
                '000703', '300488', '600939', '000799', '300347', '000419', '600671', '300219', '000078', '600191',
                '002741', '603600', '002537', '300280', '000519', '300480', '603969', '300342', '300095', '300041',
                '601928', '002441', '002555', '002530', '601991', '300638', '002567', '300576', '601616', '002009',
                '600352', '603959', '600359', '002372', '000965', '002908', '002250', '000543', '603589', '603626',
                '600482', '002130', '300137', '600403', '300613', '300193', '002564', '002298', '002846', '002566',
                '300696', '002483', '002550', '600259', '000690', '000999', '600774', '000536', '002194', '600330',
                '002214', '002360', '300339', '002156', '000877', '000929', '300675', '603101', '002205', '600115',
                '002150', '600343', '002706', '600143', '000681', '300202', '000691', '002735', '002732', '000948',
                '603138', '002578', '601369', '603828', '000065', '000767', '603903', '300282', '002229', '600729',
                '002251', '300092', '600642', '300378', '000861', '000850', '600218', '600676', '600517', '300681',
                '603859', '600327', '300259', '300125', '600429', '000700', '002678', '002749', '600510', '300457',
                '000525', '002274', '002543', '300534', '600283', '600622', '002397', '002670', '600629', '002690',
                '603035', '300321', '603696', '600372', '000980', '600262', '002685', '601588', '300214', '000757',
                '300039', '002514', '603773', '002626', '600089', '002136', '002322', '300094', '300654', '600863',
                '300661', '600312', '603416', '300171', '601929', '300148', '000625', '603887', '600535', '300410',
                '002290', '002459', '000768', '002788', '300508', '600279', '002791', '300285', '603689', '002428',
                '300389', '002093', '603168', '601558', '002006', '300420', '603986', '300585', '603586', '000166',
                '002189', '002863', '600793', '002705', '000096', '300319', '600415', '002249', '600168', '000622',
                '600098', '002687', '600053', '600719', '300360', '300408', '300348', '600855', '002027', '600765',
                '600584', '603126', '000705', '603899', '300169', '002613', '002025', '000571', '300175', '002412',
                '002052', '603970', '300021', '603069', '600816', '300025', '002385', '002651', '000007', '002682',
                '603238', '600851', '600035', '000938', '600333', '600649', '600481', '300196', '600059', '600608',
                '600550', '600433', '300170', '000598', '002135', '603117', '002529', '002384', '002880', '600491',
                '603179', '000828', '002920', '600496', '000859', '300611', '600311', '000797', '600565', '002470',
                '002347', '000987', '300271', '600967', '600526', '600186', '002067', '300693', '000010', '002047',
                '002232', '000025', '600386', '603496', '000715', '600860', '002697', '300562', '600648', '601117',
                '000880', '601519', '600639', '600370', '002324', '300498', '002740', '002695', '000546', '603616',
                '300087', '002826', '002472', '600746', '603789', '600385', '000416', '603979', '002918', '600237',
                '002734', '000836', '603196', '603336', '603033', '000875', '002928', '603896', '000428', '000788',
                '603116', '300034', '603385', '600501', '603577', '600881', '603916', '603516', '002042', '600470',
                '300281', '600315', '600161', '002107', '603198', '002406', '000915', '600973', '600452', '000918',
                '600587', '300301', '002893', '300252', '300338', '001965', '601789', '002623', '300227', '300204',
                '002403', '603359', '300358', '300659', '000982', '002478', '000695', '002899', '600790', '300367',
                '300326', '600475', '300359', '002062', '300165', '002017', '603056', '603607', '000863', '002615',
                '002818', '000888', '600845', '000882', '601199', '000637', '002254', '600838', '002503', '000826',
                '002424', '002688', '002268', '300250', '601890', '600132', '000632', '002255', '600533', '002476',
                '600422', '600173', '600070', '601515', '601238', '002708', '000778', '000785', '600388', '600018',
                '002104', '300154', '002545', '002392', '300046', '000538', '603799', '002323', '601801', '002593',
                '002032', '002442', '002202', '002513', '000034', '000668', '600178', '002154', '601965', '603557',
                '002490', '002556', '600825', '600623', '600406', '300328', '300101', '300151', '002469', '002237',
                '002191', '000407', '000012', '000030', '601607', '000589', '002420', '000157', '300114', '300013',
                '601339', '601212', '603268', '000552', '603848', '300596', '002339', '300635', '300314', '002666',
                '600418', '600769', '002166', '600757', '603878', '601020', '603703', '601058', '600858', '600836',
                '600252', '002363', '000620', '002243', '600776', '002185', '002639', '002753', '601718', '600603',
                '002760', '002178', '300377', '600739', '600778', '002434', '300011', '000889', '002430', '600118',
                '300298', '600272', '600235', '600545', '600067', '600182', '603520', '002532', '002558', '000876',
                '603355', '002491', '002781', '002559', '002396', '300673', '002654', '601969', '002647', '601177',
                '300581', '300047', '000627', '002812', '300502', '002586', '002637', '002081', '600316', '603766',
                '002350', '002448', '600467', '300720', '300234', '603980', '000701', '002275', '002689', '002167',
                '300064', '002608', '002792', '300211', '603030', '600183', '603536', '600549', '300700', '002342',
                '002847', '601231', '002700', '300345', '300300', '000856', '000688', '600381', '002481', '300631',
                '300397', '600106', '603338', '600462', '002049', '000729', '600785', '300458', '300019', '600756',
                '600012', '000737', '603966', '000719', '300599', '600961', '600996', '300189', '600435', '000411',
                '002780', '002579', '002790', '000935', '000630', '600833', '300396', '000502', '300059', '002625',
                '600100', '300026', '000159', '600805', '601019', '002771', '300717', '601717', '000605', '002284',
                '002269', '000070', '002521', '600758', '600270', '600493', '601872', '000404', '300363', '002624',
                '600068', '300147', '600681', '002137', '300447', '002522', '300237', '300651', '002825', '603933',
                '300369', '002505', '603058', '600897', '603335', '300322', '000498', '000400', '603717', '002779',
                '601333', '002316', '000860', '002816', '600824', '002003', '000027', '600072', '600029', '600382',
                '600879', '300465', '300628', '600307', '002267', '002131', '001696', '600650', '600713', '600114',
                '600634', '600284', '600873', '600242', '002215', '600988', '603060', '002599', '002528', '300203',
                '000969', '002158', '601949', '600077', '600055', '601208', '600726', '002868', '002823', '601966',
                '600200', '002057', '000883', '600717', '000516', '600872', '002755', '002701', '600099', '300163',
                '600613', '600159', '002026', '300218', '002142', '000811', '000026', '601599', '603709', '600929',
                '601158', '600604', '601326', '300276', '002220', '600373', '600179', '600249', '002072', '603609',
                '603730', '600229', '603977', '600652', '603313', '601169', '000960', '603909', '002328', '000881',
                '300365', '600127', '002379', '002557', '300387', '600894', '000978', '600983', '300481', '002910',
                '300455', '601188', '000905', '600257', '603920', '600305', '600658', '600926', '002416', '002748',
                '002526', '000760', '000638', '300199', '600761', '601689', '603128', '000585', '002561', '600576',
                '300726', '002364', '002258', '600527', '002661', '600783', '002772', '300159', '002743', '601968',
                '600171', '603181', '002583', '600612', '600177', '603260', '603042', '601127', '600368', '000702',
                '000521', '000058', '600509', '603161', '600398', '002381', '000153', '600285', '603919', '002004',
                '000928', '600123', '603517', '002109', '603083', '600090', '002216', '000582', '600993', '600162',
                '300213', '300017', '600119', '600007', '603716', '000619', '002177', '601985', '002479', '600637',
                '600980', '300737', '300334', '600190', '300432', '002365', '601106', '603628', '601866', '600356',
                '603088', '002368', '601366', '300686', '601952', '000920', '300436', '002631', '603926', '603618',
                '002066', '300105', '600592', '300589', '002473', '300446', '002307', '300587', '002642', '603005',
                '600064', '000718', '000503', '600738', '300119', '300610', '002551', '000830', '002297', '603306',
                '002391', '002329', '600170', '600025', '000722', '000068', '600712', '600105', '002489', '600882',
                '603877', '601010', '002340', '600853', '000716', '600008', '000570', '603656', '603319', '002658',
                '000547', '600827', '002759', '002527', '000541', '300251', '002912', '000952', '600320', '300140',
                '600288', '600829', '600379', '603595', '600936', '300318', '603806', '000900', '002305', '002375',
                '002402', '300623', '300664', '002320', '300215', '002862', '000401', '600820', '600770', '600725',
                '002351', '600834', '600802', '603958', '603289', '600488', '000607', '600841', '000958', '601200',
                '002060', '600986', '600169', '000970', '600269', '000899', '600513', '002709', '002664', '000612',
                '300575', '300168', '603661', '300023', '002140', '603861', '002452', '600767', '601777', '600456',
                '002153', '600577', '600080', '600108', '600661', '002540', '600995', '601798', '002717', '002600',
                '002277', '300406', '000809', '300262', '002433', '600822', '603099', '600505', '300277', '002073',
                '600360', '603987', '002827', '603817', '603318', '600409', '300103', '600624', '601128', '601107',
                '002446', '000906', '600614', '603998', '601958', '000911', '000721', '300392', '002393', '600187',
                '300516', '600959', '002039', '603726', '600137', '600396', '000897', '002046', '300703', '300421',
                '600979', '300207', '603858', '002801', '603363', '600864', '000758', '600295', '600107', '600210',
                '600336', '002087', '002832', '603208', '600255', '300117', '300027', '300145', '600353', '002628',
                '002144', '603567', '603633', '300341', '000600', '600594', '601163', '600900', '603098', '002785',
                '603881', '002404', '603189', '002019', '600819', '300240', '002378', '603229', '300571', '002703',
                '002149', '000837', '600874', '300344', '600243', '000601', '600101', '002225', '000548', '600831',
                '000066', '603006', '300102', '002036', '002455', '300045', '300016', '300190', '000665', '603639',
                '002765', '601116', '600616', '603222', '002439', '603316', '600619', '002354', '300223', '603041',
                '600595', '600120', '002467', '600111', '300182', '600139', '300417', '002718', '603596', '002344',
                '000421', '603225', '300254', '000581', '000488', '300220', '000561', '600063', '300449', '000032',
                '600723', '002163', '603680', '002266', '000530', '002315', '600908', '603167', '600480', '603298',
                '600732', '002542', '600292', '000816', '300195', '002546', '603036', '300133', '000040', '000782',
                '603345', '600275', '600220', '600026', '000099', '300526', '300077', '002362', '002091', '002325',
                '603100', '603136', '600602', '002677', '000550', '002145', '000031', '600868', '000923', '600843',
                '300161', '000417', '603017', '002373', '000524', '300273', '600817', '002534', '600798', '600747',
                '300079', '002506', '000652', '603186', '600714', '600021', '600963', '002190', '600192', '000422',
                '600426', '300496', '600010', '002786', '000766', '600309', '603677', '603882', '300222', '000759',
                '000635', '600557', '600775', '603737', '600301', '300399', '603885', '000926', '002471', '300433',
                '300386', '600691', '300248', '000520', '000517', '000089', '300414', '002681', '603020', '002157',
                '002415', '600508', '300626', '600233', '600166', '002037', '600782', '603728', '002394', '300492',
                '300051', '603218', '001896', '000017', '002242', '300474', '600561', '600355', '300268', '600096',
                '300375', '600128', '600502', '601028', '600846', '300702', '603333', '300201', '002236', '002056',
                '600523', '600421', '300043', '603869', '603655', '603356', '600479', '600239', '600662', '300443',
                '000685', '600088', '300652', '002429', '002671', '002186', '600152', '600856', '603599', '002663',
                '002283', '600203', '300063', '000610', '600056', '603066', '600857', '600617', '601518', '300191',
                '002111', '000005', '300489', '000599', '002875', '002887', '000997', '601919', '002466', '000061',
                '300577', '002495', '601579', '300441', '002435', '300591', '002155', '002038', '000568', '600528',
                '000927', '000338', '000559', '603028', '002730', '300036', '002585', '300005', '002293', '000985',
                '600779', '603663', '002079', '300331', '300106', '002407', '300024', '002805', '002034', '601168',
                '600653', '300246', '600812', '603660', '601216', '603023', '300531', '603676', '002069', '000917',
                '300658', '600416', '300287', '002562', '601377', '603178', '603131', '300733', '603608', '300556',
                '601828', '603898', '600730', '600814', '600909', '600839', '600184', '600997', '600196', '002195',
                '600704', '000712', '600647', '300231', '601877', '603797', '002094', '002001', '300275', '000592',
                '603067', '000839', '000890', '601011', '601900', '300382', '002638', '600020', '600706', '002417',
                '601002', '600696', '300138', '002535', '600459', '600366', '600400', '600039', '300519', '603698',
                '002253', '600599', '000586', '600692', '002151', '002742', '000413', '603233', '000751', '600015',
                '600635', '300053', '603380', '300583', '002012', '300052', '300002', '300249', '002494', '603778',
                '600605', '600640', '603139', '002282', '603357', '603000', '300018', '000736', '300066', '603883',
                '300530', '002605', '002196', '300445', '002444', '600721', '600919', '002187', '601788', '603050',
                '002212', '002793', '600976', '603203', '002523', '600620', '002033', '000931', '002722', '600876',
                '002738', '603063', '600560', '300329', '603328', '002105', '300313', '603015', '600830', '600219',
                '600004', '600727', '300129', '002824', '300662', '600815', '300084', '600248', '600628', '603860',
                '300349', '002659', '002201', '002088', '603612', '603022', '603879', '601211', '002204', '300330',
                '300205', '601636', '002917', '603368', '300535', '300236', '002811', '603888', '603922', '600933',
                '002174', '300380', '000609', '600589', '300741', '000858', '300033', '002544', '600707', '002533',
                '002048', '600363', '002280', '002024', '300416', '300057', '300307', '000762', '600688', '600992',
                '603177', '300099', '300503', '300428', '002015', '603326', '603200', '600674', '600875', '002591',
                '300007', '002568', '300075', '300493', '600958', '002803', '002548', '002758', '601101', '600651',
                '002736', '600458', '603538', '300528', '002837', '300533', '600195', '300305', '300289', '002425',
                '300712', '600331', '300032', '600865', '300419', '002541', '002355', '600966', '002601', '300430',
                '600463', '300210', '000680', '603678', '600861', '600755', '603360', '000528', '600743', '000623',
                '600511', '000798', '603566', '300684', '601999', '000728', '600126', '603588', '600489', '002733',
                '000021', '603585', '300022', '600530', '300245', '000988', '300265', '002645', '601007', '002272',
                '002101', '000425', '300669', '002389', '603701', '600408', '002443', '600326', '603617', '000753',
                '000049', '000961', '601198', '600901', '603031', '300695', '600748', '300494', '300160', '603993',
                '300602', '000687', '002401', '603528', '600261', '603803', '000783', '603226', '002903', '600697',
                '600082', '601618', '600051', '600057', '300437', '600308', '002643', '002649', '603358', '300364',
                '002726', '300460', '603113', '603665', '601595', '603339', '600781', '603721', '300221', '300395',
                '000930', '300580', '002577', '000953', '000937', '600287', '600895', '601328', '601015', '600050',
                '601566', '603508', '002774', '600006', '000009', '603779', '300302', '300565', '300142', '600537',
                '300336', '603159', '603937', '603330', '300563', '002221', '600209', '600668', '300405', '000597',
                '600547', '300286', '600728', '002421', '002176', '000679', '601989', '603110', '300699', '002636',
                '300401', '600351', '002620', '600590', '300263', '002768', '300029', '603699', '002116', '000662',
                '600551', '300711', '600247', '002318', '603989', '300698', '300615', '603331', '600532', '300217',
                '002707', '002338', '300353', '600516', '603286', '603990', '002750', '300442', '002787', '002367',
                '002224', '002045', '002084', '002500', '600222', '300663', '002883', '600058', '603008', '603277',
                '603303', '600898', '600837', '002160', '600428', '300515', '300158', '000692', '300127', '600690',
                '000998', '603165', '300181', '603767', '002511', '000004', '600791', '300073', '300303', '000626',
                '600138', '000962', '600073', '300178', '002331', '002553', '600705', '600588', '601811', '002867',
                '600141', '002292', '600797', '300607', '002683', '300640', '600150', '603029', '600667', '300593',
                '300070', '600556', '002731', '300507', '601111', '300255', '603106', '603002', '300738', '000531',
                '300226', '002756', '002460', '601857', '603169', '603809', '600160', '603311', '600538', '300239',
                '600871', '002106', '002261', '000657', '603322', '600854', '603615', '002629', '600037', '002729',
                '603090', '601808', '600378', '300462', '300373', '002279', '002209', '600149', '300643', '002839',
                '002930', '300020', '002117', '603976', '601666', '601179', '300058', '300671', '300431', '601555',
                '600395', '002330', '002273', '600716', '000697', '601038', '002888', '000686', '603580', '002763',
                '300497', '000885', '603366', '601918', '000514', '603917', '600512', '002169', '000780', '002807',
                '000813', '300692', '300135', '002103', '002327', '300621', '603855', '600977', '603157', '300415',
                '603999', '603527', '300037', '600552', '300258', '601898', '601608', '002244', '002669', '603738',
                '000968', '603396', '002092', '002453', '600685', '603727', '600338', '300625', '002080', '601881',
                '000887', '603912', '600390', '300648', '600425', '300394', '000709', '000554', '300297', '600016',
                '600207', '002203', '300520', '002842', '603679', '002152', '603611', '300691', '600519', '600660',
                '603089', '300750', '300209', '600582', '600571', '300155', '002536', '300483', '603309', '000893',
                '600216', '000925', '603518', '600478', '300453', '002231', '603826', '300030', '603323', '002457',
                '600703', '000617', '603685', '002018', '000100', '002595', '000551', '002845', '600172', '300608',
                '300194', '300461', '600075', '300157', '600499', '300065', '002133', '600028', '002074', '002474',
                '002817', '600135', '603166', '000655', '600225', '603499', '603978', '600708', '601229', '000558',
                '600960', '600644', '002752', '603038', '300660', '000750', '603266', '002192', '000423', '600507',
                '600985', '600885', '300641', '600736', '002597', '000050', '300504', '600665', '600609', '000852',
                '600000', '000725', '300597', '000608', '603519', '600597', '600071', '601908', '300539', '000677',
                '002751', '300279', '000671', '300370', '300485', '600663', '300295', '300208', '603321', '600487',
                '600369', '300604', '000039', '600444', '002405', '300206', '603239', '600125', '002767', '300315',
                '601500', '002366', '600711', '002574', '601677', '600679', '000717', '002064', '300590', '002644',
                '000429', '600890', '002900', '000993', '002838', '300718', '000631', '600076', '600737', '300716',
                '600999', '002696', '300115', '002337', '300435', '300316', '002118', '600206', '300697', '300525',
                '002386', '000670', '002822', '300385', '300568', '000792', '300541', '600350', '002882', '300619',
                '002684', '002233', '600847', '600198', '002698', '000037', '002414', '600281', '601800', '601766',
                '601099', '300471', '603444', '601375', '300407', '300293', '601222', '600684', '603399', '601567',
                '600332', '300605', '002409', '002217', '002123', '002456', '601611', '000878', '600583', '603129',
                '603619', '002913', '600618', '300412', '600718', '600889', '300657', '300501', '300500', '600643',
                '000060', '603227', '300710', '300444', '603079', '002855', '300624', '600823', '002120', '600322',
                '300622', '603383', '603003', '002029', '601016', '300228', '603021', '002419', '600236', '002886',
                '600083', '002657', '300426', '000565', '300085', '002499', '603637', '002789', '300184', '002648',
                '601388', '603232', '600223', '000683', '002090', '002162', '000059', '002207', '000756', '600030',
                '300109', '600410', '300735', '002715', '000158', '603559', '002468', '000045', '000800', '002820',
                '000761', '600323', '002762', '002501', '000023', '300682', '300730', '000731', '002241', '300536',
                '300722', '300479', '000835', '300429', '002002', '002635', '002929', '600887', '603603', '002857',
                '002028', '300088', '300553', '002598', '300708', '300310', '600657', '002333', '603868', '300176',
                '002745', '002497', '002835', '300381', '603188', '600558', '002890', '600808', '002061', '000532',
                '300579', '000628', '300288', '000996', '603906', '603822', '000975', '002916', '603001', '600446',
                '002508', '600157', '603026', '603269', '603507', '002571', '002891', '600234', '603955', '000795',
                '002797', '601997', '000786', '600208', '603757', '600522', '002311', '600202', '300555', '300438',
                '601218', '300678', '300467', '002208', '300031', '300010', '600742', '600117', '000672', '000833',
                '600291', '000822', '002376', '002055', '300478', '600476', '603258', '300427', '002124', '603081',
                '603308', '002919', '603378', '002926', '601288', '002308', '300230', '600693', '300272', '002285',
                '300551', '002518', '002139', '600109', '300592', '002580', '600529', '600310', '300573', '300292',
                '002172', '000055', '300600', '000789', '002594', '600031', '300055', '002892', '601311', '603908',
                '600267', '002454', '002108', '600503', '002653', '603533', '002336', '002853', '300598', '603078',
                '601899', '300511', '603123', '600866', '000573', '300506', '300243', '600277', '600397', '601001',
                '600079', '002927', '603648', '601600', '002799', '600113', '300264', '300335', '300235', '600256',
                '603712', '603458', '002531', '300212', '002423', '300690', '002902', '300616', '000831', '002612',
                '300552', '603013', '002673', '300706', '600176', '603733', '300124', '000708', '603890', '002572',
                '300629', '600091', '300545', '600773', '002112', '600321', '000720', '601838', '000913', '603500',
                '600036', '300645', '601360', '002554', '300537', '601108', '603010', '000651', '300256', '000733',
                '300557', '603320', '002896', '600531', '603223', '002245', '600466', '000848', '000676', '601988',
                '601009', '300636', '600129', '600155', '002517', '002833', '300548', '600348', '300224', '600387',
                '300687', '300061', '002884', '600151', '603505', '002850', '600420', '601186', '600121', '000898',
                '000510', '300466', '600525', '300391', '300267', '000501', '002335', '600720', '600339', '601166',
                '000028', '300038', '300014', '600970', '000537', '600877', '600581', '002213', '002095', '603900',
                '600282', '600686', '603278', '603183', '600061', '600297', '601818', '600260', '000155', '300731',
                '603118', '600777', '300340', '002860', '603515', '601225', '000959', '300655', '600520', '300096',
                '601155', '600495', '603829', '000020', '300632', '603556', '300469', '600449', '300570', '601688',
                '600749', '300089', '600741', '000555', '600497', '600546', '601006', '002077', '002869', '000529',
                '600675', '600455', '002662', '600300', '300130', '002345', '300679', '300701', '300452', '002675',
                '300617', '002008', '300656', '603895', '002879', '600683', '603813', '601066', '300423', '002770',
                '600585', '600801', '603377', '000563', '600621', '600104', '600807', '601628', '300542', '300136',
                '000908', '601088', '600971', '603833', '601336', '601398', '600094', '000732', '603801', '600231',
                '000922', '603040', '300566', '002475', '603818', '002897', '601138', '601012', '300543', '002777',
                '300422', '000807', '000851', '300409', '002873', '600962', '300668', '002713', '300707', '000895',
                '000933', '000011', '002581', '300179', '603936', '600569', '002848', '002871', '002128', '002510',
                '600610', '600362', '601003', '603180', '000932', '603283', '600337', '002122', '600180', '600539',
                '000048', '002881', '601228', '300355', '002878', '002206', '000939', '601939', '300284', '000036',
                '000983', '600468', '002260', '300560', '603729', '002889', '603535', '002247', '300164', '000090',
                '002016', '300468', '000151', '002085', '601933', '600062', '600201', '000001', '601668', '603798',
                '600859', '603086', '300402', '000659', '000014', '300274', '300650', '002050', '002343', '603839',
                '603800', '002691', '600376', '300514', '603466', '300100', '000402', '601699', '600740', '600606',
                '600188', '603686', '601233', '002798', '000951', '603758', '601318', '600019', '600153', '600702',
                '300257', '300054', '300477', '603816', '300143', '601878', '300290', '600266', '002127', '000591',
                '300434', '002146', '300473', '002126', '002828', '000063', '000006', '603108', '002876', '000921',
                '000507', '600383', '600880', '603009', '600273', '600185', '300721', '600358', '000069', '000088',
                '601601', '603938', '300729', '603365', '600258', '000333', '002110', '300667', '002482', '600325',
                '600419', '000002', '001979', '002183', '002492', '600066', '000910', '300540', '000046', '600340',
                '300376', '600438', '002341', '002313', '600734', '600048', '600439', '600787', '600892', '002256',
                '600754', '600319', '002219', '600682', '300090', '600133', '600189', '002035', '603085', '000418',
                '002271', '600568', '300028', '300266', '300393', '002193', '002766', '002699', '002711', '002164',
                '000587', '002005', '603693', '603105', '600680', '600401', '002932', '002070', '000950', '000629',
                '603713', '603706']
    p = Pool(6)
    data15 = p.map(get_15minute, stkcodes)
    save_to_mongo(data15)
    data30 = p.map(get_30minute, stkcodes)
    save_to_mongo(data30)
    data1h = p.map(get_1h, stkcodes)
    save_to_mongo(data1h)
    data1d = p.map(get_1D, stkcodes)
    save_to_mongo(data1d)
    print('insert ok')
    p.close()
    p.join()

if __name__ == '__main__':
    # main()
    max_id = 0
    wait_id = []
    flag = 0
    max_time = 0
    # 获取当前时间
    now = datetime.datetime.now()
    # 启动时间为当前时间 加2秒
    sched_timer = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute,
                                    now.second) + datetime.timedelta(seconds=2)
    while (True):
        # 当前时间
        now = datetime.datetime.now()
        if sched_timer < now < sched_timer + datetime.timedelta(seconds=1):
            time.sleep(1)
            print(now)
            # 运行程序
            main()
            # 将标签设为 1
            flag = 1
        else:
            if flag == 1:
                # 修改定时任务时间 时间间隔为1分钟
                sched_timer = sched_timer + datetime.timedelta(minutes=12)
                flag = 0